package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.comment.domain.TravelComment;
import cn.wolfcode.luowowo.comment.query.TravelCommentQuery;
import cn.wolfcode.luowowo.comment.service.ITravelCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.UMEditorUploader;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("travel")
public class TravelController {

    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelCommentService travelCommentService;

    @RequestMapping("")
    public String list(@ModelAttribute("qo") TravelQuery qo, Model model) {
        qo.setState(Travel.STATE_RELEASE);
        model.addAttribute("pageInfo", travelService.query(qo));
        return "travel/list";
    }

    //需求：显示游记的detail页面
    @RequestMapping("/detail")
    public String detail(Long id, Model model) {
        //游记（travel）detail
        Travel detail = travelService.get(id);

        //将游内容设置到Travel中
        TravelContent content = travelService.getContent(id);
        detail.setTravelContent(content);
        model.addAttribute("detail", detail);
        //目的地吐司 toasts
        List<Destination> toasts = destinationService.getToasts(detail.getDest().getId());
        toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);

        //阅读量前3的攻略明细sds
        List<StrategyDetail> strategyDetails = strategyDetailService.selectDetailTop3ByDestId(detail.getDest().getId());
        model.addAttribute("sds", strategyDetails);

        //阅读量前3的游记 travel
        model.addAttribute("ts", travelService.getHotTravelTop3(detail.getDest().getId()));


        //list评论列表
        TravelCommentQuery qo = new TravelCommentQuery();
        qo.setTravelId(id);
        qo.setPageSize(Integer.MAX_VALUE);
        Page<TravelComment> page = travelCommentService.query(qo);
        model.addAttribute("list", page.getContent());
        return "travel/detail";
    }

    @Value("${file.path}")
    private String filePath;

    //上传图片
    @RequestMapping("/coverImageUpload")
    @ResponseBody
    public Object coverImageUpload(MultipartFile pic) {
        String upload = UploadUtil.upload(pic, filePath);
        return upload;
    }

    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception {
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile, filePath);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize()
                + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
        result = result.replaceAll("\\\\", "\\\\");
        if (callback == null) {
            return result;
        } else {
            return "<script>" + callback + "(" + result + ")</script>";
        }
    }

    /**
     * 编写文章的页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/input")
    public String input(Model model, Long id) {

        //tv
        if (id != null) {
            Travel travel = travelService.get(id);
            TravelContent content = travelService.getContent(id);
            travel.setTravelContent(content);
            model.addAttribute("tv", travel);
        }


        //regions(目的地)
        List<Destination> regionId = destinationService.getDestByDeep(3);
        model.addAttribute("regions", regionId);

        return "travel/input";
    }

    /**
     * 保存操作
     *
     * @param
     * @param travel
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Travel travel, @UserParam UserInfo userInfo) {

        Long travelId = travelService.saveOrUpdate(travel);


        AjaxResult ajaxResult = new AjaxResult().addData(travelId);
        return ajaxResult;
    }

    /**
     * 游记的评论的保存
     * @param travelComment
     * @param userInfo
     * @return
     */
    @RequestMapping("/commentAdd")

    public String commentAdd(Model model,int floor,TravelComment travelComment, @UserParam UserInfo userInfo) {
        travelComment.setUserId(userInfo.getId());
        travelComment.setHeadUrl(userInfo.getHeadImgUrl());
        travelComment.setLevel(userInfo.getLevel());
        travelComment.setCity(userInfo.getCity());
        //评论人的信息
        TravelComment c = travelCommentService.save(travelComment);
        //评论数
        model.addAttribute("floor", floor + 1);
        //评论的对象
        model.addAttribute("c", c);
        return "travel/commentTpl";
    }
}



